Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM Computer
10-Jun-2025CSRF (Cross-Site Request Forgery) is a type of web security vulnerability where a malicious website tricks a user's browser into making unauthorized requests to a different site where the user is already authenticated.
Example of a CSRF Attack:
Let’s say:
https://mybank.com.http://evil.com.mybank.comwith that request — even though the request came from a malicious site.➡ Result: Money transferred without user’s intention.
CSRF Is a Threat Only When:
How to Protect APIs from CSRF
SameSite=StrictorLaxso they're not sent on cross-origin requests.GET(safe) unless authenticated.Most Secure Setup for APIs
If your API is:
Authorizationheaders instead of cookies.Why JWT is safer for APIs?
Authorizationheader by JavaScript:In Summary
Origin/RefererIf your Web API is authenticated using cookies, and it supports browser-based users, you should implement CSRF tokens.
If you're building a RESTful API for SPAs or mobile apps, use bearer tokens or API keys — then CSRF is not a concern.
Would you like a code example of CSRF protection in: